<?php

use yii\helpers\Html;
use yii\widgets\ActiveForm;
use yii\web\View;
use common\models\FormVal;
use common\models\goods\GoodsAttr;


$ga=new GoodsAttr;

$skus=$ga->getSKUs();
$specs=$ga->getSPECs();

//下拉列表中删除已经选择的属性
if(!$model->isNewRecord){
    $maps=$model->maps;
    
    $attrIds=[];
    foreach ($maps as $map) {
        $attrIds[]=$map->attr_id;
    }
    
    foreach ($skus as $i=>$sku) {
        if(in_array($sku->id, $attrIds)){
            unset($skus[$i]);
        }
    }
    
    foreach ($specs as $i=>$spec) {
        if(in_array($spec->id, $attrIds)){
            unset($specs[$i]);
        }
    }
}
?>

<style>
    .red{color:red;font-weight:normal;}
    .attr-list,.add-button{margin-left:30px;}
    .attr-list li{margin:8px auto;}
    .attr-list .sort{width:40px;margin:auto 9px;}
</style>

<div class="goods-type-form">

    <?php $form = ActiveForm::begin(); ?>

    <?= $form->field($model, 'name')->textInput(['maxlength' => 45]) ?>

    <?= $form->field($model, 'enable')->radioList(FormVal::$bool, ['separator' => FormVal::$separator10]); ?>

    <div class="form-group field-goodstype-attrs">
        <label class="control-label" for="field-goodstype-attrs">销售属性<span class="red">(排序值越大越靠前)</span></label>
        
        <div id="skuList" class="attr-list">
            <?php if(!$model->isNewRecord):?>
                <?php foreach ($maps as $map):?>
                    <?php if($map->attr->type==GoodsAttr::TYPE_SKU):?>
                    <li>
                        <span><?=$map->attr->name?></span> 
                        <input type="hidden" name="attrs[id][]" value="<?=$map->attr_id?>" /> 
                        <input type="text" name="attrs[sort][]" value="<?=$map->sort_order?>" placeholder="排序" class="sort" /> 
                        <input type="hidden" name="attrs[group][]" />
                        <a href="javascript:void(0);" onclick="del(this,'skuSelect');">删除</a>
                    </li>
                    <?php endif;?>
                <?php endforeach;?>
            <?php endif;?>
        </div>
        
        <div class="add-button">
            <select id="skuSelect">
                <?php foreach ($skus as $sku):?>
                <option value="<?=$sku->id?>"><?=$sku->name?></option>
                <?php endforeach;?>
            </select> 
            <input type="button" value="添加" id="addSKU" />
        </div>
        
        <div class="hint-block" style="margin-left:30px;">请不要随意添加、删除销售属性或修改销售属性排序。操作不当，将无法找到货品，操作请慎重！</div>
        <div class="help-block"></div>
    </div>
    
    <div class="form-group field-goodstype-attrs">
        <label class="control-label" for="field-goodstype-attrs">扩展属性<span class="red">(排序值越大越靠前)</span></label>
         
        <div id="specList" class="attr-list">
            <?php if(!$model->isNewRecord):?>
                <?php foreach ($maps as $map):?>
                    <?php if($map->attr->type==GoodsAttr::TYPE_SPEC):?>
                    <li>
                        <span><?=$map->attr->name?></span> 
                        <input type="hidden" name="attrs[id][]" value="<?=$map->attr_id?>" /> 
                        <input type="text" name="attrs[sort][]" placeholder="排序" class="sort" value="<?=$map->sort_order?>" /> 
                        <input type="text" name="attrs[group][]" placeholder="分组名" class="group" value="<?=$map->group_name?>" /> 
                        <a href="javascript:void(0);" onclick="del(this,'specSelect');">删除</a>
                    </li>
                    <?php endif;?>
                <?php endforeach;?>
            <?php endif;?>
        </div>
        
        <div class="add-button">
            <select id="specSelect">
                <?php foreach ($specs as $spec):?>
                <option value="<?=$spec->id?>"><?=$spec->name?></option>
                <?php endforeach;?>
            </select> 
            <input type="button" value="添加" id="addSPEC" />
        </div>
        <div class="help-block"></div>
    </div>
    
    <div class="form-group">
        <?= Html::submitButton($model->isNewRecord ? '新建' : '更新', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
    </div>

    <?php ActiveForm::end(); ?>

</div>

<?php
$this->registerJsFile('@web/js/goods/type.js',['depends'=>[backend\assets\AppAsset::className()],'position'=>View::POS_END]);
?>
